const {
  querySql,
  queryOne,
  updateSql,
  insertSql,
  deleteSql,
} = require("../db");

function getClassList(query, teacherIdArr) {
  const { className, classCode, semesterId, pageNum, pageSize } = query;
  return querySql(
    `select * from class_list where 1=1 ${`and className like '%${
      className || ""
    }%'`} ${classCode ? `and classCode like '%${classCode}%'` : ""}  ${
      semesterId ? `and linkSemester = ${semesterId}` : ""
    } ${
      teacherIdArr && teacherIdArr.length > 0
        ? `and (${teacherIdArr
            .map(
              (id) =>
                `teacherList like '%,${id},%' or teacherList like '%${id},%' or teacherList like '%,${id}\]%' or teacherList like '%\[${id}\]%'`
            )
            .join(" or ")})`
        : ""
    }   order by id desc limit ${(pageNum - 1) * pageSize},${pageSize}`
  );
}

function getMyClassList(id) {
  return querySql(
    ` select * from class_list left join examination_list on class_list.id = examination_list.classId where teacherList like '%,${id},%' or teacherList like '%\[${id},%' or teacherList like '%,${id}\]%' or teacherList like '%\[${id}\]%' `
  );
}

function getTotalCount(query, teacherIdArr) {
  const { className, classCode, semesterId } = query;
  return queryOne(
    `select count(*) as totalCount from class_list where 1=1 ${`and className like '%${
      className || ""
    }%'`} ${classCode ? `and classCode like '%${classCode}%'` : ""}  ${
      semesterId ? `and linkSemester = ${semesterId}` : ""
    } ${
      teacherIdArr && teacherIdArr.length > 0
        ? `and (${teacherIdArr
            .map(
              (id) =>
                `teacherList like '%,${id},%' or teacherList like '%${id},%' or teacherList like '%,${id}\]%' or teacherList like '%\[${id}\]%'`
            )
            .join(" or ")})`
        : ""
    } `
  );
}

function editClass(query) {
  query.updateTime = new Date();
  query.teacherList = JSON.stringify(query.teacherList);
  return updateSql(query, "class_list", `where id = ${query.id}`);
}

function createClass(query, cardId) {
  query.createTime = new Date();
  query.createId = cardId;
  query.teacherList = JSON.stringify(query.teacherList);
  return insertSql(query, "class_list");
}

function editExam(query, int) {
  return updateSql(query, "examination_list", `where int = ${int}`);
}

function createExam(query) {
  return insertSql(query, "examination_list");
}

function deleteClass(query) {
  return deleteSql("id", query.id, "class_list");
}

function getClassInfoById({ id }) {
  return queryOne(`select * from class_list where id = ${id}`);
}

function getClassSchedule(id) {
  return querySql(
    `select classSchedule_data.*,classroom_list.roomName,classroom_list.roomCode,classSection_list.sectionName,classSection_list.startTime,classSection_list.finalTime  from classSchedule_data left join classroom_list on classSchedule_data.linkClassRoomId  = classroom_list.id left join classSection_list on classSchedule_data.linkSectionId  = classSection_list.id where linkClassId = ${id}`
  );
}

function editClassSchedule(query) {
  return updateSql(query, "classSchedule_data", `where id = ${query.id}`);
}

function createClassSchedule(query) {
  return insertSql(query, "classSchedule_data");
}

function deleteClassSchedule(query) {
  return deleteSql("id", query.id, "classSchedule_data");
}

module.exports = {
  getClassList,
  editClass,
  createClass,
  deleteClass,
  getClassInfoById,
  getTotalCount,
  getClassSchedule,
  editClassSchedule,
  createClassSchedule,
  deleteClassSchedule,
  getMyClassList,
  createExam,
  editExam,
};
